package com.think.teaching.service.impl;

import com.think.teaching.model.CourseType;
import com.think.teaching.mapper.CourseTypeMapper;
import com.think.teaching.service.CourseTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程类型 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class CourseTypeServiceImpl extends BaseServiceImpl<CourseTypeMapper, CourseType> implements CourseTypeService {
	@Override
	public CourseType AddCourseType(CourseType courseType) {
		if (this.save(courseType)) {
			return courseType;
		}
		return null;
	}

	@Override
	public CourseType UpdateCourseType(CourseType courseType) {
		if (this.updateById(courseType)) {
			return courseType;
		}
		return null;
	}

	@Override
	public CourseType GetCourseType(Integer id) {
		QueryWrapper<CourseType> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	@Override
	public List<CourseType> SearchCourseTypeWithKey(String key,Boolean withoutBan) {
		QueryWrapper<CourseType> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("type_name", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return list(queryWrapper);
	}

	@Override
	public IPage<CourseType> ListCourseTypeWithTagAndType(Integer count, Integer page, Integer type, Integer tag,Boolean withoutBan) {
		QueryWrapper<CourseType> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		IPage<CourseType> ipage = new Page<>(page, count);
		return this.page(ipage,queryWrapper);
	}

	@Override
	public CourseType SetCourseTypeState(Integer id, Integer state) {
		UpdateWrapper<CourseType> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}
	
	public CourseType GetCourseTypeByName(String name) {
		QueryWrapper<CourseType> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("type_name", name).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}
}
